//
//  STAnimationViewController.swift
//  iPhone
//
//  Created by Toj on 8/8/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STAnimationViewController: STBaseViewController {
   

    private lazy var startButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("动画 开始", for: .normal)
        view.addTarget(self, action: #selector(didTapStart), for: .touchUpInside)
        return view
    }()

    private lazy var stopButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("动画 结束", for: .normal)
        view.addTarget(self, action: #selector(didTapStot), for: .touchUpInside)
        return view
    }()
    
    /* <#NOTE#> */
    private lazy var loadingView: STAnimationLoadingController = {
        let vc = STAnimationLoadingController()
        vc.view.isHidden = true
        return vc
    }()
}

// MARK: - 生命周期
extension STAnimationViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CATransitionType(rawValue: "")
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension STAnimationViewController {
    
    func didTapStart() {
       
        loadingView.loadDataRequest()
    }
    
    func didTapStot() {
        loadingView.view.isHidden = false
        
        var loadingViewFrame = loadingView.view.frame
        let loadingViewY = CGFloat.dv_height - loadingViewFrame.height
        loadingViewFrame.origin.y = loadingViewY
        UIView.animate(withDuration: 4) {
            self.loadingView.view.frame = loadingViewFrame
        }
    }
}

// MARK: - 子类可重写方法/ objc事件
@objc extension STAnimationViewController {
    
}

// MARK: - Public
extension STAnimationViewController {
    
}

// MARK: - Private
private extension STAnimationViewController {
    
}

// MARK: - 初始化设置
private extension STAnimationViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(startButton)
        view.addSubview(stopButton)
        
        
        addChild(loadingView)
        view.addSubview(loadingView.view)
    }
    
    func setupMakeLayoutSubviews() {
        
        let navY = navigationController?.nav_barFullHeight ?? 0
        
        let column = 2
        let width = 150.to_cgfloat
        let height = 44.to_cgfloat
        let radius = height * 0.5
        
        let marginY = 30.to_cgfloat
        let marginX = (CGFloat.sr_width - CGFloat(column) * width) / (CGFloat(column + 1))
        var x = CGFloat.zero
        var y = CGFloat.zero
        let subViews = [
            startButton,
            stopButton
        ]
        var indexI = 0
        subViews.forEach { subview in
            let clm = indexI % column
            let row = indexI / column
            x = marginX + CGFloat(clm) * (marginX + width)
            y = navY + CGFloat(row) * (marginY + height)
            subview.frame  = CGRect(x: x, y: y, width: width, height: height)
            
            subview.cm_shadow(color: UIColor.black.tt_alpha(0.4), opacity: 1, radius: 5, offset: CGSize(width: 0, height: 3), corner: radius)
            
            indexI += 1
            y += height
        }
        
        y = y + 100
       let loadingViewHeight = CGFloat.dv_height - y
        loadingView.view.frame = CGRect(x: 0, y: CGFloat.dv_height, width: CGFloat.dv_width, height: loadingViewHeight)
    }
    
    func setupMakeInitialize() {
       loadingView.loadDataRequest()
    }
}
